'use strict';
const db = uniCloud.database()
const dbCmd = db.command
let articleArr = null;
let msg = null;
exports.main = async (event, context) => {
	const {
		articleId,
		userId
	} = event;
	//获取用户数据列表
	const userInfo = await db.collection('user').doc(userId).get();
	const articleIds = userInfo.data[0].article_likes_ids;
	//判断数据表的指定数据是否收藏了这个文章
	if (articleIds.includes(articleId)) {
		articleArr = dbCmd.pull(articleId);
		msg = '取消收藏成功'
	} else {
		articleArr = dbCmd.addToSet(articleId);
		msg = '收藏成功'
	};
	//修改当前这个指定的用户的收藏文章状态
	await db.collection('user').doc(userId).update({
		article_likes_ids: articleArr
	})
	//重新获取用户信息，返回前端
	const updateUser = await db.collection('user').doc(userId).get()

	return {
		code: 0,
		data: {
			msg,
			newUserInfo: updateUser.data[0],
		}
	}
};